﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DALScolarite;
namespace Scolarite 
{
    public partial class EnseignantGroupe : Form
    {
        public EnseignantGroupe()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            scolarite_enseignants ens = new scolarite_enseignants();
            List<scolarite_enseignants> listens = ens.Enseignants();
            
            foreach (scolarite_enseignants eltens in listens) { 

          

                ListViewItem item1 = new ListViewItem();
                item1.SubItems.Add(eltens.idEnseignants.ToString());
                item1.SubItems.Add(eltens.CIN );
                item1.SubItems.Add(eltens.NomFR );
                item1.SubItems.Add(eltens.PrenomFR ); 
                item1.SubItems.Add(eltens.Email );

                this.listView1.Items.Add(item1);
            }

        }

        private void EnseignantGroupe_Load(object sender, EventArgs e)
        {
            scolarite_eltdiplome dip = new scolarite_eltdiplome();

            scolariteanneeunivBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_anneeuniv.ToList();
            scolariteeltdiplomeBindingSource.DataSource = dip.diplome();

            scolaritesemestresBindingSource.DataSource = GlobalParams.MonDbcontext.scolarite_semestres.ToList();

         
        
        }

        private void idEltDiplomeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            scolarite_classes classe = new scolarite_classes();
            scolarite_ensunites ensunit = new scolarite_ensunites();
            if (idEltDiplomeComboBox.SelectedValue != null  )
            {
                scolariteclassesBindingSource.DataSource = classe.classe(idEltDiplomeComboBox.SelectedValue.ToString());
                scolariteensunitesBindingSource.DataSource = ensunit.unite(idEltDiplomeComboBox.SelectedValue.ToString());
            }
        }

        private void SemestrecomboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            scolarite_ensunites ensunit = new scolarite_ensunites();
            if (idEltDiplomeComboBox.SelectedValue != null && SemestrecomboBox1.SelectedValue != null)
            {
                scolariteensunitesBindingSource.DataSource = ensunit.uniteSemestre(idEltDiplomeComboBox.SelectedValue.ToString(), SemestrecomboBox1.SelectedValue.ToString());
            }
        }

        private void btdetailselection_Click(object sender, EventArgs e)
        {
            scolarite_groupeclass grp = new scolarite_groupeclass();
            if (idEltDiplomeComboBox.SelectedValue != null && SemestrecomboBox1.SelectedValue != null && idClasseComboBox1.SelectedValue!=null && idAnneeUnivComboBox.SelectedValue!=null)
            {
                scolaritegroupeclassBindingSource.DataSource = grp.groupeclasse(idClasseComboBox1.SelectedValue.ToString());
                etudiantsInscriptDataGridView.DataSource = grp.EtudiantParGroupe(idEltDiplomeComboBox.SelectedValue.ToString(), idClasseComboBox1.SelectedValue.ToString(), idAnneeUnivComboBox.SelectedValue.ToString());
            }
        }
        /*****Select and deselect items********************/
        private void btselectdomaine_Click(object sender, EventArgs e)
        {
            List<scolarite_groupeclass> grp = new List<scolarite_groupeclass>(); 
           

            for (int x = listBox1.Items.Count - 1; x >= 0; x--)
            {
                if (listBox1.GetSelected(x))
                {
                    
                    listBox2.Items.Add(listBox1.Items[x]);

                    // listBox1.Items.RemoveAt(idx);
                }
                else
                {
                    grp.Add((scolarite_groupeclass)listBox1.Items[x]);
                }
            }

            
            listBox2.DisplayMember = "NomDeGroupe";
            listBox2.ValueMember = "idGroupeClass";


            listBox1.DataSource = null;
            listBox1.Items.Clear();
            foreach (var elt in grp)
            {
                listBox1.Items.Add(elt);
            }
           
            listBox1.DisplayMember = "NomDeGroupe";
            listBox1.ValueMember = "idGroupeClass";
             
        }

        private void btdeselectdomaine_Click(object sender, EventArgs e)
        {
            List<scolarite_groupeclass> grp = new List<scolarite_groupeclass>();


            for (int x = listBox2.Items.Count - 1; x >= 0; x--)
            {
                if (listBox2.GetSelected(x))
                { 
                    listBox1.Items.Add(listBox2.Items[x]);  
                }
                else
                {
                    grp.Add((scolarite_groupeclass)listBox2.Items[x]);
                }
            } 

            listBox1.DisplayMember = "NomDeGroupe";
            listBox1.ValueMember = "idGroupeClass";


            listBox2.Items.Clear();
            foreach (var elt in grp)
            {
                listBox2.Items.Add(elt);
            }
           
            listBox2.DisplayMember = "NomDeGroupe";
            listBox2.ValueMember = "idGroupeClass";
        }
        /************************END select and deselect items**************************/
        private void btsave_Click(object sender, EventArgs e)
        {
            ///tester soit Creation de Contextcohorte ou utilisation de Contextcohort de groupe pour chaque groupe 
            //
            scolarite_contextcohorts context = new scolarite_contextcohorts();
            uvtmoyEntities ent = new uvtmoyEntities();
            if (listView1.CheckedItems.Count == 1)
            {
                int idense;
                int.TryParse(listView1.CheckedItems[0].SubItems[1].Text, out idense);

                scolarite_contextcohorts cohort ;

                foreach(scolarite_groupeclass itemgroupe in listBox2.Items){
                    ///Ajouter le contextcohort
                    cohort = context.contextcohorts(itemgroupe.idGroupeClass);
                    long idcontextcohort; 
                    if (cohort != null)
                    {
                        idcontextcohort = cohort.idContextCohorts; 
                    }
                    else
                    {
                        cohort = new scolarite_contextcohorts{
                            idScolarite_TypeCohorts ="Groupe",
                            refcontextcohorts =(itemgroupe.idClasse + " : " + itemgroupe.NomDeGroupe )
                        };
                        ent.scolarite_contextcohorts.Add(cohort);
                        ent.SaveChanges();
                      scolarite_groupeclass modifgroup =   ent.scolarite_groupeclass.Find(itemgroupe.idGroupeClass);


                         modifgroup.idContextCohorts = cohort.idContextCohorts;
                        ent.scolarite_groupeclass.Attach(modifgroup);
                        ent.SaveChanges();

                        idcontextcohort = cohort.idContextCohorts;
                    }
                    //insertion des ensseignant selon le se qu'il vont enseiger
                    foreach (scolarite_eltetude itemeltetude in listBox4.Items)
                    {
                        scolarite_enseigneunite ensunit = new scolarite_enseigneunite
                        {
                            idAnneeUniv = idAnneeUnivComboBox.SelectedValue.ToString(),
                            idContextCohorts = idcontextcohort,
                            idEltEtude = itemeltetude.idEltEtude,
                            idEnseignants = idense,
                            idunite = ModulecomboBox1.SelectedValue.ToString()
                        };
                        ent.scolarite_enseigneunite.Add(ensunit);

                    }
                    ent.SaveChanges();
                  
                
                }
                MessageBox.Show("Insertion effetuer avec succée");
            }
            else
            {
                MessageBox.Show("Selectionner un seul enseignant");

            }



        }

        private void btselecttypeeltens_Click(object sender, EventArgs e)
        {
            List<scolarite_eltetude> grp = new List<scolarite_eltetude>();


            for (int x = listBox3.Items.Count - 1; x >= 0; x--)
            {
                if (listBox3.GetSelected(x))
                {

                    listBox4.Items.Add(listBox3.Items[x]);

                    // listBox1.Items.RemoveAt(idx);
                }
                else
                {
                    grp.Add((scolarite_eltetude)listBox3.Items[x]);
                }
            }


            listBox4.DisplayMember = "idTypeEltEtude";
            listBox4.ValueMember = "idEltEtude";


            listBox3.DataSource = null;
            listBox3.Items.Clear();
            foreach (var elt in grp)
            {
                listBox3.Items.Add(elt);
            }

            listBox3.DisplayMember = "idTypeEltEtude";
            listBox3.ValueMember = "idEltEtude";
        }

        private void btdesselecttypeeltens_Click(object sender, EventArgs e)
        {
            List<scolarite_eltetude> grp = new List<scolarite_eltetude>();


            for (int x = listBox4.Items.Count - 1; x >= 0; x--)
            {
                if (listBox4.GetSelected(x))
                {
                    listBox3.Items.Add(listBox4.Items[x]);
                }
                else
                {
                    grp.Add((scolarite_eltetude)listBox4.Items[x]);
                }
            }


            listBox3.DisplayMember = "idTypeEltEtude";
            listBox3.ValueMember = "idEltEtude";


            listBox4.Items.Clear();
            foreach (var elt in grp)
            {
                listBox4.Items.Add(elt);
            }

            listBox4.DisplayMember = "idTypeEltEtude";
            listBox4.ValueMember = "idEltEtude";
        }

        private void ModulecomboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            scolarite_eltetude typeelt = new scolarite_eltetude();
            if (ModulecomboBox1.SelectedValue != null)
            {
                scolariteeltetudeBindingSource.DataSource = typeelt.eltetudeunite(ModulecomboBox1.SelectedValue.ToString());
            }
        }

       
    }
}
